package com.guochao.service.impl;

import java.util.*;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.guochao.dao.ProductMapper;
import com.guochao.entity.ClassIfication;
import com.guochao.entity.Page;
import com.guochao.entity.Product;
import com.guochao.service.ProductService;

@Service
public class ProductServiceIMPL implements ProductService {

    @Resource
    private ProductMapper productMapper;

    @Override
    public Product proSelByCId(Integer pid) {
        return productMapper.proSelByCId(pid);
    }

    @Override
    public List<Map<String, Object>> proSelByCIds(Integer[] pid, int[] pronum, String[]size) {
        /* 调用 Dao 层从数据库获取相应商品列表 */
        int id;
//        List<Product> proinfo = productMapper.proSelByCIds(pid);
        List<Product> proinfo=new LinkedList<>();
        for (int i = 0; i <pid.length ; i++) {
            id=pid[i];
            proinfo.add(productMapper.proSelByCId(id));
        }

        /* 创建新的键值对集合，存放信息 */
        List<Map<String,Object>> prolist=new ArrayList<Map<String, Object>>();
        for(int i=0;i<pid.length;i++){
            Product product=new Product();
            product.setPid(pid[i]);
//            返回对象 product 在 proinfo 集合中出现的索引位置
            int j=proinfo.indexOf(proinfo.get(i));
            System.out.println("---Svc---："+product);
            System.out.println("---Svc---："+proinfo.get(i));
            System.out.println("---Svc---："+j);
            if(j!=-1){
                Product pro=proinfo.get(i);
                Map<String,Object> m=new HashMap<String, Object>();
                m.put("pid", pid[i]);
                m.put("pname", pro.getPname());
                m.put("price", pro.getPrice());
                m.put("pimg", pro.getPimg());
                m.put("pronum", pronum[i]);
                m.put("size",size[i]);
                prolist.add(m);
            }
        }
        return prolist;
    }

    @Override
    public int proSelCount(Integer classid) {
        return productMapper.proSelCount(classid);
    }

    @Override
    public List<Product> findPage(Page page) {
        return productMapper.findPage(page);
    }

    @Override
    public ClassIfication findClassName(Integer classid) {
        return productMapper.findClassName(classid);
    }

    @Override
    public int proSelAllCount() {
        return productMapper.proSelAllCount();
    }

    @Override
    public List<Product> findAllPage(Page page) {
        return productMapper.findAllPage(page);
    }

    @Override
    public List<Product> selProductAll() {
        List<Product> result = null;
        result=productMapper.selProductAll();
        return result;
    }

    @Override
    public int upProduct(Product product) {
        return productMapper.upProduct(product);
    }

    @Override
    public List<Product> selNumProduct(int index) {
        return productMapper.selNumProduct(index);
    }

    @Override
    public List<Product> selTypePro(String type, int index, String sort) {
        return productMapper.selTypePro(type, index, sort);
    }

    @Override
    public List<Product> searchProduct(String keyname, Integer classid) {
        return productMapper.searchProduct(keyname, classid);
    }

    @Override
    public int addProduct(Product product) {
        return productMapper.addProduct(product);
    }

    @Override
    public int delProduct(Integer pid) {
        return productMapper.delProduct(pid);
    }

    @Override
    public Boolean delProducts(String ids) {
        Boolean result = false;

        if (ids.contains("-")) {
            //批量删除
            String[] str_ids = ids.split("-");
            List<Integer> list_ids = new ArrayList<Integer>();
            for (String str_id : str_ids) {
                list_ids.add(Integer.parseInt(str_id));
            }
            productMapper.delStatesByList(list_ids);
            result = true;
        } else {
            //普通删除
            Integer id = Integer.parseInt(ids);
            productMapper.delProduct(id);
            result = true;
        }
        return result;
    }


}
